import { _decorator, Component, Label, Node, Sprite } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('CardItemController')
export class CardItemController extends Component {
  @property(Label)
  cardIndex: Label;
  @property(Label)
  title: Label;
  @property(Label)
  content: Label;
  @property(Sprite)
  img: Sprite;
  @property(Node)
  mask: Node;
  @property(Node)
  dark: Node;

  used: boolean = false;
  position: -1 | 1 = -1; // 卡片移动方向，已方卡片1，对方卡片-1
  Dist: number = 30; // 卡片移动距离

  start() {}
  init({ cardIndex, title, content, img, position, dark }: any = {}) {
    if (cardIndex) this.cardIndex.string = cardIndex;
    if (title) this.title.string = title;
    if (content) this.content.string = content;
    if (img) this.img.spriteFrame = img;
    if (position) this.position = position;
    // 是否暗牌
    this.dark.active = this.position === -1 && dark;
    // 重置状态
    this.used = false;
    // 隐藏遮罩
    this.mask.active = false;
    // 重置位置
    this.node.setPosition(this.node.position.x, 0);
  }
  // 标记为已使用
  markCardUsed() {
    this.used = true;
    this.mask.active = true;
    this.node.setPosition(this.node.position.x, 0);
  }
  // 修改为选中状态
  select() {
    if (this.used) return;
    this.node.setPosition(this.node.position.x, this.Dist * this.position);
  }
  // 修改为未选中状态
  unSelect() {
    if (this.used) return;
    this.node.setPosition(this.node.position.x, 0);
  }
  // 暗牌状态
  setDark() {
    this.dark.active = this.position === -1;
  }
  // 非暗牌状态
  setUnDark() {
    this.dark.active = false;
  }

  update(deltaTime: number) {}
}
